package com.ruihang.generalibrary.ui.util;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;

import com.jiongbull.jlog.JLog;

import java.lang.ref.WeakReference;

/**
 * Created by rh008 on 2017/7/25.
 */

public class TimerViewHelper<T extends View> {

    public static final long DEFAULT_REFRESH_INTERVAL = 1000L;

    private final int WHAT_START=201;
    private final int WHAT_REFRESH=202;
    private final int WHAT_STOP=203;

    private WeakReference<T> wt;

    private Handler handler;

    private TimerViewCallback<T> callback;

    private long refreshInterval;

    private boolean isRunning;

    public TimerViewHelper(T t) {
        this.wt = new WeakReference<T>(t);
        handler=new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                if (msg!=null) {
                    parseMsg(msg);
                }
            }
        };
    }

    private void parseMsg(Message msg){
        switch (msg.what){
            case WHAT_START:
                handler.removeMessages(WHAT_REFRESH);
                handler.removeMessages(WHAT_START);
                handler.removeMessages(WHAT_STOP);
                JLog.e("msg start");
                if (callback!=null){
                    isRunning=true;
                    handler.sendEmptyMessageDelayed(WHAT_REFRESH,refreshInterval);
                    callback.onTimerStart(wt.get(),this);
                }
                break;
            case WHAT_REFRESH:
                handler.removeMessages(WHAT_REFRESH);
                handler.sendEmptyMessageDelayed(WHAT_REFRESH,refreshInterval);
//                JLog.e("msg refresh");
                if (callback==null || !callback.onTimerRefresh(wt.get(),this,refreshInterval)){
                    handler.sendEmptyMessage(WHAT_STOP);
                }
                break;
            case WHAT_STOP:
                handler.removeMessages(WHAT_REFRESH);
                handler.removeMessages(WHAT_START);
                handler.removeMessages(WHAT_STOP);
//                JLog.e("msg stop");
                isRunning=false;
                if (callback!=null){
                    callback.onTimerStop(wt.get(),this);
                }
                break;
        }
    }

    public void startTimer(){
        startTimer(DEFAULT_REFRESH_INTERVAL);
    }

    public synchronized void startTimer(long refreshInterval) {
        if (refreshInterval>=500){
            this.refreshInterval=refreshInterval;
            handler.sendEmptyMessage(WHAT_START);
        }
    }

    public void stopTimer(){
        handler.sendEmptyMessage(WHAT_STOP);
    }

    public void onDestory(){
        stopTimer();
        callback=null;
    }

    public void setCallback(TimerViewCallback<T> callback) {
        this.callback = callback;
    }

    public boolean isRunning() {
        return isRunning;
    }

    public interface TimerViewCallback<T extends View>{
        void onTimerStart(T t,TimerViewHelper<T> helper);

        /**
         *
         * @param t
         * @param helper
         * @return 是否继续
         */
        boolean onTimerRefresh(T t,TimerViewHelper<T> helper,long timeInterval);
        void onTimerStop(T t,TimerViewHelper<T> helper);
    }

}
